const urllib=require('url');
const http=require('http');
const https=require('https');
const cheerio=require('cheerio');
const fs = require("fs");

function get(url){
	let urlObj= urllib.parse(url);
	let mothed = http;
	if(urlObj.protocol!="http:"){
		mothed = https;
	}
let req = mothed.request({
 	 host: urlObj.host,
      path: urlObj.path,
	},res=>{
		if(res.statusCode==200||res.statusCode==301||res.statusCode==302)
	})

req.on('error', err=>{
  console.log('错了', err);
});
req.write('');      //发送POST数据
req.end();          //正式开始请求


}


//get("https://www.baidu.com/img/bd_logo1.png?where=super");
get("https://www.jd.com/");


/*
let {host,path} = urllib.parse("http://www.baidu.com/");
http.get({
 host: host,
  path: path,
}, function(res) {
	console.log(res.statusCode)
  let arr=[];
	res.on('data', data=>{
	  arr.push(data);
	});
	
	res.on('end', ()=>{
	  let str=Buffer.concat(arr).toString();
		let $ = cheerio.load(str);
		$("img").each(function(){
			let url =$(this).attr("src");
			let {host,path} = urllib.parse(url);
			let name = path.substring(path.lastIndexOf("/")+1);
			console.log(name)
			http.get({
				 host: host,
				  path: path,
				},res=>{
					console.log(res.statusCode)
					let arr=[];
					res.on('data', data=>{
					  arr.push(data);
					});
					res.on('end', (res)=>{
						fs.writeFile("img/"+name, Buffer.concat(arr),null,err=>{
							console.log(err)
						})
					})
				})
		})
	});
}).on('error', function(e) {
  console.log("错误：" + e.message);
});*/